package de.rosstauscher.jculfw.homematic.events;



/**********************************************************
 * An event for a "Remote" or "Push Button" press.
 * @author Rossi java@rosstauscher.de
 *********************************************************/

public class HomeMaticRemoteButtonEvent extends HomeMaticEvent {
	
	private int buttonNo;
	private int eventID;
	private HomeMaticButtonState state;

	/**********************************************************
	 * Constructor
	 * @param length
	 * @param count
	 * @param control
	 * @param msgType
	 * @param sender
	 * @param destination
	 * @param payLoad
	 *********************************************************/
	
	public HomeMaticRemoteButtonEvent(String length, String count, String control,
			String msgType, String sender, String destination, String payLoad) {
		super(length, count, control, msgType, sender, destination, payLoad);
		
		int btnPart = Integer.parseInt(payLoad.substring(0, 2), 16);
		this.buttonNo = ( (btnPart & 0x3f) + 1) / 2;
		this.eventID = Integer.parseInt(payLoad.substring(2), 16);

		String stateString = ( (btnPart & 1) > 0 ? "OFF" : "ON") + ( (btnPart & 0x40) > 0 ? "_LONG" : "");
		this.state = HomeMaticButtonState.valueOf(stateString);
	}

	/**********************************************************
	 * If the "remote" has more than one button this identifies 
	 * the button that was pressed.
	 * Note that 2 hardware buttons can be mapped to the same 
	 * button number.
	 * e.g. the first one as an ON button and the second one 
	 * as a OFF button. 
	 * 
	 * @return the buttonNo of the button that was pressed.
	 *********************************************************/
	
	public int getButtonNo() {
		return buttonNo;
	}

	/**********************************************************
	 * The event ID of the press event. If the button is kept
	 * pressed and "repeats" then the event ID will be the same
	 * as the last one. Please not that the eventID itself has 
	 * only 255 values and will then start from the beginning.
	 * @return the eventID of the press event.
	 *********************************************************/
	
	public int getButtonEventID() {
		return eventID;
	}

	/**********************************************************
	 * The button state. 
	 * @return the command/state that should be switched too.
	 *********************************************************/
	
	public HomeMaticButtonState getButtonState() {
		return state;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "HomeMaticRemoteButtonEvent [buttonNo=" + buttonNo
				+ ", buttonEventID=" + eventID + ", buttonState=" + state
				+ ", sender=" + getSender() + ", destination="
				+ getDestination() + ", senderDeviceType="
				+ getSenderDeviceType() + ", totalTelegramCount="
				+ getTotalTelegramCount() + "]";
	}

}
